# -*- coding: utf-8 -*-
# __author__ = "maple"


from django.shortcuts import render
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
from tracer.settings import BUCKET
logging.basicConfig(level=logging.INFO, stream=sys.stdout)

secret_id = 'AKIDkGXZPzHQL2T7WioOr9E4lEVXKyNbAZIo'      # 替换为用户的 secretId
secret_key = 'RhiT1JTN4YnnERTQJyOZWDftzpaDdrrf'      # 替换为用户的 secretKey
region = 'ap-beijing'     # 替换为用户的 Region
token = None                # 使用临时密钥需要传入 Token，默认为空，可不填
scheme = 'https'            # 指定使用 http/https 协议来访问 COS，默认为 https，可不填
config = CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key, Token=token, Scheme=scheme)
# 2. 获取客户端对象
client = CosS3Client(config)

#上传
with open(r'C:\Users\zhoujinghua\Desktop\pythonS25学习计划\每日代码&笔记\day139 & saas\saas day01 笔记和代码\豆瓣源.txt', 'rb') as fp:
    response = client.put_object(
        Bucket=BUCKET,
        Body=fp,
        Key='豆瓣源.txt',
        StorageClass='STANDARD',
        EnableMD5=False
    )
print(response['ETag'])

#下载
response = client.get_object(
    Bucket='tracer-1301482037',
    Key='豆瓣源.txt',
)
response['Body'].get_stream_to_file('output.txt')